<?php

require_once 'classes/LightPressPlugin.php';

class PostTags extends LightPressPlugin {

    var $default_context = LP_CONTEXT_ALL;
    var $active = true;
    var $hooks = array('get_posts_loop', 'parse_post');

    var $description = 'Displays the tags associated with each post.';
    var $constructor_args = array(
        'tag_separator' => 'Text/html used to separate displayed keywords',
        'tag_none' =>      'Text/html to display if there are no keywords for the post',
        'tags_in_feeds' => 'Retrieve tags in feeds instead of only retrieving them for blog pages'
        );
    
    var $_frontend;
	
    // options
	var $tag_separator = ', ';
	var $tag_none = 'none';
    var $tags_in_feeds = true;

	var $_block_set = false;		// set when keywords block is initialized
    var $_cachedtags = null;
	
    function PostTags(&$frontend, $args, $dummy_run=false) {
		$this->LightPressPlugin($frontend, $args, $dummy_run);
        if (!$dummy_run && !$this->tags_in_feeds && $frontend->view_type != 'html')
            $this->active = false;
	}
    
    function run($hook, &$post) {
        if (!$this->active)
            return ($this->hide());
        
        $f =& $this->_frontend;
        $tpl =& $f->tpl;
		
        switch ($hook) {
            case 'get_posts_loop':
                
                // force tag cache load and assign tags to post
                $tags =& $f->getPostTags($post['post_id']);
                
                if (count($tags) == 0 || (isset($post['_tags_set']) && $post['_tags_set']))
                    return;
                
                if (isset($post['tags']) && is_array($post['tags'])) {
                    $post['tags'] = array_merge($post['tags'], $tags);
                    $post['tags'] = array_unique($post['tags']);
                } else {
                    $post['tags'] =& $tags;
                }
                
                break;
            case 'parse_post':
                        
                if ($f->view_type != 'html')
                    return;
                
                if (!isset($post['tags']) || count($post['tags']) == 0) {
                    $tpl->setVar('PLUGIN_POSTTAGS', '');
                } else  {
                    // process tags block for each post
                    
                    // initialize post template block
                    if (!$this->_block_set) {
                        $tpl->setFile('plugin_posttags', 'plugins/post_tags.xml');
                        $tpl->setBlock('plugin_posttags', 'post_tag', 'BLOCK_POST_TAGS');
                        $this->_block_set = true;
                    }
                            
                    // process meta data
                    $appendblock = false;
                    if (isset($post['tags'])) { 
                        foreach($post['tags'] as $tag) {
                            $tpl->setVar(array(
                                'post_tag_name' => $tag,
                                'post_tag_encoded' => $f->getTagEncoded($tag),
                                'post_tag_permalink' => $f->getTagPermalink($tag),
                                'post_tag_separator' => $appendblock ? $this->tag_separator : ''));
                            $tpl->parse('BLOCK_POST_TAGS', 'post_tag', $appendblock);
                            $appendblock = true;
                        }
                    }
                    if ($appendblock)
                        $tpl->parse('PLUGIN_POSTTAGS', 'plugin_posttags');
                    else
                        $tpl->setVar('PLUGIN_POSTTAGS', $this->tag_none);
                    break;
                }
                
                break;
        }
    }
    
	function hide() {
        $tpl =& $this->_frontend->tpl;
		// hide template tags
        $tpl->setVar('PLUGIN_POSTTAGS', '');
    }
}

?>
